if (get_cookie("key")) {
    $.ajax({
        url: ip + "index.php?s=admincms/login/isadmin",
        type: "post",
        data: {
            "user": get_cookie("user"),
            "key": get_cookie("key")
        },
        success: function (_res) {
            res = JSON.parse(_res);
            if (res.resStatus === 0) {
                // 用户写入页面
                $("#user_name").text(res.data[0].NickName);
                $(".content_left>ul").empty();
                var url=location.href;
                var tiao=true;
                $.each(res.data[0].power_list,function (i,o) {
                    if(url.indexOf(o.url)!==-1){
                        tiao=false;
                    }
                    var _background_=  url.indexOf(o.url)!==-1?'style="background:#000;"':"";

                    $(".content_left>ul").append('<a href="'+o.url+'"> <li '+_background_+'><span class="glyphicon '+o.icon+'"></span><p>'+o.names+'</p></li></a>');

                });

                if(tiao){
                    location.href=res.data[0].power_list[0].url;
                }
            }
            if (res.resStatus === 1) {
                location.href = key_address;
            }
        },
        error: function () {
            location.href = key_address;
        }
    });
} else {
    location.href = key_address;
}
$(function () {
    var houtai = {
        linshi_search: {
            user_type: "",
            search: ""
        },
        linshi_id: null, //临时的id存储 用来判断同一个按钮的操作id
        linshi_arrid: [],
        repertory_type: null, // 1拉黑 2取消拉黑 提交时根据类型判断请求
        //提示功能
        hint:function (str) {
            $(".hint").remove();
            $("body").append("<div class='hint'>"+str+"</div>");
            $(".hint").slideDown(300).delay(2000).slideUp();
        },
        //防止频繁操作
        old_click_time: 1000,
        click_yes: function () {
            var now_tiem = new Date().getTime();
            if (now_tiem - houtai.old_click_time > 1000) {
                houtai.old_click_time = now_tiem;
                return true;
            } else {
                return false;
            }
        },
        // 检查功能一般检查是否为空
        jiancha: function () {
            for (var i = 0; i < $(".add_goods_body .input_is:visible").length; i++) {
                if ($(".add_goods_body .input_is:visible").eq(i).val() === "") {
                    $(".add_goods_body .input_is:visible").eq(i).focus();
                    houtai.hint($(".add_goods_body .input_is:visible").eq(i).prev().text() + " 没有填写！");
                    return false;
                }
            }
            return true;
        },
        // 获取商品的数组
        shop_list_ajax: function (page_number) {
            var n = page_number ? page_number : 1;
            $.ajax({
                url: ip + 'index.php?s=admincms/member/show',
                type: "post",
                data: {
                    serach: houtai.linshi_search.search,
                    page: n,
                    islock: houtai.linshi_search.user_type,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res = JSON.parse(_res);
                    if (!page_number && res.resStatus === 0) {
                        houtai.hint("<span style='color:#0f0;'>为你找到" + res.totalpage + "条数据</span>");
                    }
                    houtai.table_string(res);
                        $("#page").sdpagination({
                            boxClass: 'sdpagination',
                            totalCount: res.totalpage,           //总条数
                            pageSize: 10,                //每页显示
                            pageIndex: n,                //当前页数
                            preText: '上页',                                  //上一页按钮显示的字符
                            nextText: '下页',                                 //下一页按钮显示的字符
                            firstText: '首页',                                //首页按钮显示的字符
                            lastText: '末页',                                 //末页按钮显示的字符
                            showNearby: 3,                                    //当前按钮前后按钮数
                            infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                            pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                            onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                                houtai.shop_list_ajax(pageIndex);
                            }
                        });

                }

            });
        },
        // 把获取的商品数组写入table
        table_string: function (res) {
            console.log(res);
            // 写入table
            var string_head = "<thead> <tr> <th>名称</th> <th>可用金豆</th>  <th>冻结金豆</th>  <th>当前积分</th> <th>总积分</th><th>积分历史</th> <th>注册时间</th>  <th>操作</th>   </tr> </thead>";
            var string_body = "";
            if (res.resStatus === 1) {
                string_body = "<tr class='text-center'><td colspan='6'>没有数据</td></tr>";
            } else if (res.resStatus === 0) {
                $.each(res.data, function (i, o) {
                    var ruku = o.score_record.addTime === 0 ? "无" : '<div class="item_history"> <div title="点击查看所有历史"> <b><span>订单号:</span></b><span title="查看全部历史"class="btn btn-info btn-xs inbound_all">看全部</span> <p>' + o.score_record.orderid + '</p> </div> <p class="fl"><b>积分：</b><span>' + o.score_record.score + '</span></p><p class="fr">' + o.score_record.addTime + '</p> </div> ';
                    var _islock = o.islock === 0 ? '<b class="btn btn-danger btn-xs add_hei">加入黑名单</b>' : '<b class="btn btn-success btn-xs cancle_hei">取消黑名单</b>';
                    string_body += '<tr>' +
                        '<td class="user_id hidden">' + o.id + '</td>' +
                        '<td class="user_name" ">' + o.user + '</td>' +
                        '<td class="user_available" ">' + o.usableBean + '</td>' +
                        '<td class="user_freeze" ">' + o.notUseBean  + '</td>' +
                        '<td class="user_now_jifen" ">' + o.now_score + '</td>' +
                        '<td class="user_tatle_jifen" ">' + o.total_score + '</td>' +
                        '<td style="width: 300px;vertical-align: top;" class="inbound_history" >' + ruku + '</td>' +
                        '<td class="user_time">' + o.time + '</td>' +
                        '<td style="width:100px;">' + _islock + '</td> </tr>';
                });
            }

            $("#repertory_table").empty().append(string_head + string_body);
        },
        //初始类
        chushi_class: function () {
            $.ajax({
                url: ip + "index.php?s=admincms/product/product",
                async: false,
                type: "post",
                data: {
                    token:get_cookie("key")
                },
                success: function (res) {
                    if (JSON.parse(res).resStatus === 0) {
                        houtai.arr = JSON.parse(res).data;
                        if (!sessionStorage.one) {
                            sessionStorage.one = houtai.arr[0].pid;
                        }
                        if (!sessionStorage.two) {
                            sessionStorage.two = houtai.arr[0].activity_list[0].cid;
                        }
                        for (var ii = 0, string11 = ""; ii < houtai.arr.length; ii++) {
                            string11 += '<option value="' + houtai.arr[ii].pid + '">' + houtai.arr[ii].typesnames + '</option>';
                        }
                        $("#select1").empty().append(string11);
                        for (var b = 0; b < houtai.arr.length; b++) {
                            if (houtai.arr[b].pid == sessionStorage.one) {
                                break;
                            }
                        }
                        for (var aa = 0,
                                 string22 = '<option value="">全部</option>'; aa < houtai.arr[b].activity_list.length; aa++) {
                            string22 += '<option value="' + houtai.arr[b].activity_list[aa].cid + '">' + houtai.arr[b].activity_list[aa].classnames + '</option>';
                        }
                        $("#select2").empty().append(string22);
                        $("#select1").val(sessionStorage.one);
                    }
                }
            });
        },
        // 请求积分历史的ajax
        jifen_history: function (pages) {
            var n = pages ? pages : 1;
            $.ajax({
                url: ip + "index.php?s=admincms/member/score",
                type: "post",
                data: {
                    uid: houtai.linshi_id,
                    page: n,
                    token:get_cookie("key")
                },
                success: function (_res) {
                    var res=JSON.parse(_res);
                    houtai.jifen_history_string(res);
                    $("#page_inbound").sdpagination({
                        boxClass: 'sdpagination',
                        totalCount: res.totalpage,           //总条数
                        pageSize: 10,                //每页显示
                        pageIndex: n,                //当前页数
                        preText: '上页',                                  //上一页按钮显示的字符
                        nextText: '下页',                                 //下一页按钮显示的字符
                        firstText: '首页',                                //首页按钮显示的字符
                        lastText: '末页',                                 //末页按钮显示的字符
                        showNearby: 3,                                    //当前按钮前后按钮数
                        infoTemplet: '每页{pageSize} 共{totalCount} 当前{pageIndex}/{pageCount}',//自定义信息模板
                        pageList: [5, 10, 20],                            //设置PageSize下拉框的选择项，
                        onPageIndexChange: function (pageIndex) { //回调，点击按钮时或者输入页数时，激发onPageIndexChange事件，回调参数为 新页数、PageSize
                            houtai.jifen_history(pageIndex);
                        }
                    });

                }
            });
        },
        //积分历史详情table
        jifen_history_string: function (res) {
            var data = res.data;
            var name;
            $.each($("#repertory_table tr .user_id"), function (i, o) {
                if ($(o).text() === houtai.linshi_id) {
                    name = $(o).parents("tr").find(".user_name").text();

                }
            });
            // 写入table
            var string_head = "<thead> <tr> <th>名称</th>  <th>积分</th> <th>订单号</th><th>生成时间</th> </tr> </thead>";
            var string_body = "";
           if (res.resStatus === 0) {
                $.each(data, function (i, o) {
                    string_body += '<tr>' +
                        '<td>' + name + '</td>' +
                        '<td>' + o.score + '</td>' +
                        '<td>' + o.orderid + '</td>' +
                        '<td style="width:40%">' + o.addTime + '</td>' +
                        '</tr>';
                });
            }else{
                string_body = "<tr class='text-center'><td colspan='4'>没有找到哦 亲！</td></tr>";
                houtai.hint("没有找到哦 亲!");
            }
            $("#table_history").empty().append(string_head + string_body);
            $("#repertory_table_history").fadeIn();
        },
        //用户拉黑  取消拉黑
        user_status_ajax: function () {
            //拉黑
            if (houtai.repertory_type === 1) {
                $.ajax({
                    url: ip + "index.php?s=admincms/member/isLock",
                    type: "post",
                    data: {
                        id: houtai.linshi_id,
                        tp: 0,
                        token:get_cookie("key")
                    },
                    success: function (_res) {
                        var res=JSON.parse(_res);
                        $("#repertory_inbound").fadeOut();
                        if (res.resStatus === 0) {
                            houtai.hint("<span style='color:#0f0;'>加入黑名单成功</span>");
                            $.each($("#repertory_table tr .user_id"), function (i, o) {
                                if ($(o).text() === houtai.linshi_id) {
                                    $(o).parents("tr").find(".add_hei").removeClass("add_hei btn-danger").addClass("cancle_hei btn-success").text("取消黑名单");
                                    $("#modal_wrap").hide();
                                }
                            });
                        }else{
                            houtai.hint("操作失败了！！");
                        }
                    },
                    error:function () {
                        houtai.hint("操作失败了！！");
                    }
                });
            }
            //取消拉黑
            if (houtai.repertory_type === 2) {
                $.ajax({
                    url: ip + "index.php?s=admincms/member/isLock",
                    type: "post",
                    data: {
                        id: houtai.linshi_id,
                        tp: 1,
                        token:get_cookie("key")
                    },
                    success: function (_res) {
                    var res=JSON.parse(_res);
                        if (res.resStatus === 0) {
                            houtai.hint("<span style='color:#0f0;'>取消黑名单成功</span>");
                            $.each($("#repertory_table tr .user_id"), function (i, o) {
                                if ($(o).text() === houtai.linshi_id) {
                                    $(o).parents("tr").find(".cancle_hei").removeClass("cancle_hei btn-success").addClass("add_hei btn-danger").text("加入黑名单");
                                    $("#modal_wrap").hide();
                                }
                            });
                        }else{
                            houtai.hint("操作失败了！！");
                        }

                    },
                    error:function () {
                        houtai.hint("操作失败了！！");
                    }
                });
            }
        }
    };
    houtai.chushi_class();
    houtai.linshi_search.class1 = $("#select1").val();
    houtai.linshi_search.class2 = $("#select2").val();
    houtai.shop_list_ajax();
// 收展事件
    $(".right-top>b").bind("click", function () {
        if ($(".content_left").is(":hidden")) {
            $(".content_left").removeClass("ishidden");
            $(".content_right").removeClass("iskuozhang");
        } else {
            $(".content_left").addClass("ishidden");
            $(".content_right").addClass("iskuozhang");
        }
    });
    // 用户状态选择
    $("#user_select").bind("change", function () {
        houtai.linshi_search.user_type = $("#user_select").val();
        houtai.linshi_search.search = "";
        houtai.shop_list_ajax();
    });
    //修改事件
    $("#set_add").bind("click", function () {
        if (houtai.jiancha()) {
            houtai.xiugai();
        }
    });
    // 莫态框取消modal
    $("#modal_close").bind("click", function () {
        $("#modal_wrap").fadeOut(200);

    });
    // 修改取消
    $(".cancle_set").bind("click", function () {
        $(".set_shangpin").fadeOut(200);

    });
    //历史查询弹框关闭
    $("#close_table_history").bind("click", function () {
        $("#repertory_table_history").fadeOut(200);
    });
    //查看全部入库历史
    $("#repertory_table").on("click", ".inbound_history .inbound_all", function () {
        houtai.linshi_id = $(this).parents("tr").find(".user_id").text();
        houtai.jifen_history();
    });
    //拉黑按钮
    $("#repertory_table").on("click", ".add_hei", function () {
        houtai.linshi_id = $(this).parents("tr").find(".user_id").text();
        houtai.repertory_type = 1;
        $(".modal-body").empty().append("你确定要拉黑<span style='color: #ff1100;'>" + $(this).parents("tr").find(".user_name").text() + "</span>吗？");
        $("#modal_wrap").show();
    });
    //取消拉黑按钮
    $("#repertory_table").on("click", ".cancle_hei", function () {
        houtai.linshi_id = $(this).parents("tr").find(".user_id").text();
        houtai.repertory_type = 2;
        $(".modal-body").empty().append("你确定将<span style='color: #ff1100;'>" + $(this).parents("tr").find(".user_name").text() + "</span>移除黑名单吗？");
        $("#modal_wrap").show();
    });
    // 确认按钮
    $("#confirm").bind("click", function () {
        houtai.user_status_ajax();
    });
    //出入库确认按钮
    $(".add").bind("click", function () {
        if (houtai.click_yes()) {
            if (houtai.jiancha()) {
                houtai.repertory_ajax();
            }
        }

    });
    //搜索库存商品
    $("#search_btn").bind("click", function () {
        if ($("#search").val() === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.user_type = "";
            houtai.linshi_search.search = $("#search").val();
            houtai.shop_list_ajax();
        }
    });
    //回车按钮操作
    $("#search_kucun_form").submit(function () {
        if ($("#search").val() === "") {
            houtai.hint("请输入后再查询");
            $("#search").focus();
        } else {
            houtai.linshi_search.user_type = "";
            houtai.linshi_search.search = $("#search").val();
            houtai.shop_list_ajax();
        }
        return false;
    });
    //退出按钮
    $("#exit").bind("click", function () {
        set_cookie("key", "", 0);
        location.href = key_address;
    });
    //头像按钮
    $(".right-top>div").bind("click", function (e) {
        $("#exit").slideToggle();
        e.stopPropagation();
    });
    $(window).bind("click", function () {
        $("#exit").slideUp();
    });
});
